##PLot personality traits by sample
##AGES sample, national population sample, AMOS sample, Law Student sample
library(ggplot2)
library(jtools)
library(scales)
library(stargazer)
library(rockchalk)
library(effects)
library(rio)
library(standardize)
library(car)
library(scales)
library(readstata13)
library(tidyverse)
library(lvplot)
library(cowplot)
library(grid)
library(gridExtra)
library(ggridges)
library(data.table)
library(viridis)
library(hrbrthemes)

##Read in AGES data
d2=read.dta13("C:/Users/mrm32/Dropbox/Political Ambition/Replication Files/Political Behavior (PersonalityAmbition)/AGESReplicationData.dta")
##make matching variable names
d2$open_s=scale(d2$Openness)
d2$consc_s=scale(d2$Conscientiousness)
d2$extra_s=scale(d2$Extraversion)
d2$agree_s=scale(d2$Agreeableness)
d2$Neuroticism=20-d2$Neuroticism
d2$stable_s=scale(d2$Neuroticism)
##subset the data
d2b5 <- d2[c(543:559)]

##Read in the 2019 national survey data
d3 = read.dta13("C://Users//mrm32//Dropbox//Political Ambition//Replication Files//Political Behavior (PersonalityAmbition)//NSReplicationData.dta")
##make matching variable names
d3$open_s= d3$open
d3$consc_s= d3$consc
d3$agree_s= d3$agree
d3$extra_s= d3$extra
d3$stable_s= d3$emotstab
##subset the data
d3b5 <- d3[c(681:685)]
##Read in the 2022 Law Student survey data
d4 = read.dta13("C://Users//mrm32//Dropbox//Political Ambition//Replication Files//Political Behavior (PersonalityAmbition)//LSReplicationData.dta")
##make matching variable names
d4$open_s= d4$open
d4$consc_s= d4$consc
d4$agree_s= d4$agree
d4$extra_s= d4$extra
d4$stable_s= d4$emotstab
#subset the data
d4b5 <- d4[c(120:124)]
##Create variable indicating the dataset the variable comes from
#d1b5$sample=c("Local Officials")
d2b5$sample=c("Government Employees")
d3b5$sample=c("General Population")
d4b5$sample=c("Law Students")

##Append the datasets
d5= rbind(d3b5,d2b5)
d6= rbind(d5,d4b5)
d7=as.data.frame(d6)
#d7$sample=factor(d7$sample)

d7$open_s[d7$open_s< -3]=NA
d7$consc_s[d7$consc_s< -3]=NA
d7$extra_s[d7$extra_s< -3]=NA
d7$agree_s[d7$agree_s< -3]=NA
d7$stable_s[d7$stable_s< -3]=NA

##Make the figure
##Openness
ggplot(d7, aes(x=open_s,y=sample,fill=sample, stat="binline")) +
  geom_density_ridges() +
  theme_ridges() + 
  labs(title = paste("Openness by Sample"),
       y = paste(""),
       x = paste(""))+
  scale_fill_grey()+
  scale_color_grey()+
  theme(legend.position = "none")

##Conscientiousness
ggplot(d7, aes(x=consc_s,y=sample,fill=sample, stat="binline")) +
  geom_density_ridges() +
  theme_ridges() + 
  labs(title = paste("Conscientiousness by Sample"),
       y = paste(""),
       x = paste(""))+
  scale_fill_grey()+
  scale_color_grey()+
  theme(legend.position = "none")

##Extraversion
ggplot(d7, aes(x=extra_s,y=sample,fill=sample, stat="binline")) +
  geom_density_ridges() +
  theme_ridges() + 
  labs(title = paste("Extraversion by Sample"),
       y = paste(""),
       x = paste(""))+
  scale_fill_grey()+
  scale_color_grey()+
  theme(legend.position = "none")

#Agreeableness
ggplot(d7, aes(x=agree_s,y=sample,fill=sample, stat="binline")) +
  geom_density_ridges() +
  theme_ridges() + 
  labs(title = paste("Agreeableness by Sample"),
       y = paste(""),
       x = paste(""))+
  scale_fill_grey()+
  scale_color_grey()+
  theme(legend.position = "none")

#Emotional Stability
ggplot(d7, aes(x=stable_s,y=sample,fill=sample, stat="binline")) +
  geom_density_ridges() +
  theme_ridges() + 
  labs(title = paste("Emotional Stability by Sample"),
       y = paste(""),
       x = paste(""))+
  scale_fill_grey()+
  scale_color_grey()+
  theme(legend.position = "none")
